<?php

namespace app\system\controller;

use app\common\model\AdminModel;


class AdminController extends BaseController
{

  /**
   * 不需要登陆的方法
   * @var array
   */
  protected $noLogin = [];

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = ['info', 'change'];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
      'add'  => 'index',
      'edit' => 'index',
      'set'  => 'index',
      'del'  => 'index',
  ];

  /**
   * 当前模型
   * @var null
   */
  protected $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new AdminModel();
  }

  /**
   * 首页
   * @return mixed
   * @throws \Exception
   */
  public function index()
  {
    if ($this->request->isAjax()) {
      $query = $this->getUidQuery('id');
      $data = $this->getPage(null, [], [],$query);
      if (!empty($data['rows'])) $data['rows'] = $this->model->_formatList($data['rows']);
      return $data;
    }
    return $this->fetch();
  }

  /**
   * 添加用户
   * @return string
   * @throws \Exception
   */
  public function add()
  {
    if($this->loginInfo['account_type']==3) abort(500,'子账号不允许添加账号!');
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'username|用户名' => 'require',
          'password|密码'  => 'require',
          'name|姓名'      => 'require',
      ]);
      if($this->loginInfo['account_type']==2){
        //总账号只能添加子账号
        $data['account_type'] = 3;
        $data['pid'] = $this->loginInfo['uid'];
        //用户组继承总账号
        $data['group_id'] = $this->model->where('id',$this->loginInfo['account_type'])->value('group_id');
      }
      $this->model->add($data);
      $this->success('操作成功');
    }
    return $this->fetch();
  }

  /**
   * 修改用户
   * @return string
   * @throws \Exception
   */
  public function edit()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'id|用户标识'      => 'number',
          'username|用户名' => 'require',
          'name|姓名'      => 'require',
      ]);
      if($this->loginInfo['account_type']==2 && $this->loginInfo['uid']==$data['id']){ //总账号状态不允许修改
        $data['status'] = 1;
      }
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $id   = $this->request->param('id/d');
    $info = $this->model->where('id', $id)->find();
    $this->assign('info', $info);
    return $this->fetch();
  }


  /**
   * 修改会员状态
   * @throws \Exception
   */
  public function set()
  {
    $id = $this->request->param('id/d');
    if ($id == 1) abort(500, '系统内置管理员不允许更新');
    $info   = $this->model->where('id', $id)->find();
    if($info['account_type']==2 && $this->loginInfo['uid']==$id) abort(500,'总账号状态不允许修改');
    $status = $info['status'] ? 0 : 1;
    $this->model->where('id', $id)->update(['status' => $status]);
    $this->success('操作成功');
  }

  /**
   * 修改账户信息
   * @return string
   * @throws \Exception
   */
  public function info()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'username|用户名' => 'require|length:4,32',
          'name|姓名'      => 'require',
      ]);
      $data['id'] = $this->loginInfo['uid'];
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $info = $this->model->where('id', $this->loginInfo['uid'])->find();
    $this->assign('info', $info);
    return $this->fetch();
  }

  /**
   * 修改密码
   * @return string
   * @throws \Exception
   */
  public function change()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'old_password|原密码'           => 'require|length:6,32',
          'new_password|新密码'           => 'require|length:6,32',
          'new_password_confirm|确认新密码' => 'require|confirm',
      ]);
      if ($data['old_password'] == $data['new_password']) $this->error('新密码和旧密码相同');
      $this->model->change($this->loginInfo['uid'], $data['old_password'], $data['new_password']);
      $this->success('密码修改成功，下次登录生效');
    }
    $info = $this->model->where('id', $this->loginInfo['uid'])->find();
    $this->assign('info', $info);
    return $this->fetch();
  }

  /**
   * 删除
   * @throws \Exception
   */
  public function del()
  {
    $id = $this->request->param('id/d');
    $info = $this->model->where('id',$id)->find();
    if($info['account_type']==2 && $this->loginInfo['uid']==$id) abort(500,'总账号不允许删除');
    $this->model->del($id);
    $this->success('操作成功');
  }
}
